<?php

namespace app\user\controller;


use app\common\controller\User;
use think\Db;


class Safe extends User
{
    //修改资料
    public function index()
    {
        $this->assign('st', []);
        return $this->fetch();
    }

    /**
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function user_pass()
    {
        if ($this->request->isPost()) {
            $params = $this->request->param();

            if ($params['password1'] != $params['password2']) {
                $this->error('两次密码输入不一致！');
            }

            $result = \app\common\serve\User::resets($this->uid, $params['password1']);

            if (is_array($result) && $result['code'] == 1) {
                $this->success($result['msg'], url('index'));
            } else {
                $this->error($result['msg']);
            }

        }

        return $this->fetch();
    }

    /**
     * 支付密码
     * @return array|mixed
     */
    public function pay_pass()
    {
        $db = Db::name('Members');
        $phone = $db->where(array('id' => $this->uid))->value('user_phone');

        if ($this->request->isPost()) {

            if (!checkSms($phone, $_POST['scode'])) return ['code' => 0, 'msg' => '短信验证码不正确', 'data' => null];

            $pass = input('post.pass');
            $pass2 = input('post.pass1');

            if (!preg_match('/^[0-9]\d{5}$/', $pass) || !preg_match('/^[0-9]\d{5}$/', $pass2))
                return ['code' => 0, 'msg' => '支付密码格式不正确'];

            if (md5($pass) != md5($pass2))
                return ['code' => 0, 'msg' => '两次支付密码不同'];

//            $old_pass = $db->where(['id' => $this->uid])->value('pin_pass');
            if (\app\common\serve\User::checkPayPass($this->uid, $pass)) return ['code' => 0, 'msg' => '新支付密码和原支付密码不得相同'];

            if (\app\common\serve\User::setPayPass($this->uid, $pass2)) {
                return ['code' => 1, 'msg' => '重置支付密码成功', 'url' => url('index')];
            } else {
                return ['code' => 0, 'msg' => '重置支付密码失败', 'data' => null];
            }
        }

        $this->assign('phone', $phone);
        return $this->fetch();
    }

    /**
     * 收货地址
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add_ress()
    {
        if ($this->request->isPost()) {

            $params = $this->request->param();

            $result = \app\common\serve\User::address([
                'id' => $params['id'],
                'uid' => $this->uid,
                'name' => $params['name'],
                'phone' => $params['phone'],
                'address' => $params['address'],
                'way' => $this->isMobile() ? 2 : 1
            ]);

            if (is_array($result) && $result['code'] == 1) {
                $this->success($result['msg'], url('index'));
            } else {
                $this->error($result['msg']);
            }
        }

        $address = model('UserAddress')->where(['uid' => $this->uid])->select();

        if (count($address, 1) > 0) {
            $this->assign('address', $address);
        } else {
            $this->assign('address', []);
        }

        return $this->fetch();
    }

    /**
     * 删除
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del_address()
    {
        $id = $this->request->param('id');
        if ($id < 1) $this->error('参数出错！');

        $flag = Db::name('user_address')->where(['id' => $id, 'uid' => $this->uid])->delete();
        if ($flag) {
            $this->success('删除成功', 'add_ress');
        }
        $this->success('删除失败', 'add_ress');
    }

    /**
     * 绑卡
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function bank()
    {

        if ($this->request->isPost()) {

            $params = $this->request->param();
            $params['uid'] = $this->uid;

            $result = \app\common\serve\User::bindBank($params);

            if (is_array($result) && $result['code'] == 1) {
                $this->success($result['msg'], 'bank');
            } else {
                $this->success($result['msg'], 'bank');
            }
        }


        $list = Db::name('user_bank')->where(['uid' => $this->uid])->select();
        $this->assign('list', $list);
        $this->assign('bank_list', config('bank.bank_list'));
        $this->assign('way', model('UserBank')->types);

        return $this->fetch();
    }
}
